home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2017 October / PCgo 10-2017 CD-ROM Germany.iso / nw.pak / Unnamed File 000127.txt < prev    next >
Encoding:
Text File  |  2015-07-29  |  3.2 KB  |  118 lines

  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. cr.define('cr.ui', function() {
  6.   /**
  7.    * A class to manage grid of focusable elements in a 2D grid. For example,
  8.    * given this grid:
  9.    *
  10.    *   focusable  [focused]  focusable  (row: 0, col: 1)
  11.    *   focusable  focusable  focusable
  12.    *   focusable  focusable  focusable
  13.    *
  14.    * Pressing the down arrow would result in the focus moving down 1 row and
  15.    * keeping the same column:
  16.    *
  17.    *   focusable  focusable  focusable
  18.    *   focusable  [focused]  focusable  (row: 1, col: 1)
  19.    *   focusable  focusable  focusable
  20.    *
  21.    * And pressing right at this point would move the focus to:
  22.    *
  23.    *   focusable  focusable  focusable
  24.    *   focusable  focusable  [focused]  (row: 1, col: 2)
  25.    *   focusable  focusable  focusable
  26.    *
  27.    * @param {Node=} opt_boundary Ignore focus events outside this node.
  28.    * @param {cr.ui.FocusRow.Observer=} opt_observer An observer of rows.
  29.    * @implements {cr.ui.FocusRow.Delegate}
  30.    * @constructor
  31.    */
  32.   function FocusGrid(opt_boundary, opt_observer) {
  33.     /** @type {Node|undefined} */
  34.     this.boundary_ = opt_boundary;
  35.  
  36.     /** @private {cr.ui.FocusRow.Observer|undefined} */
  37.     this.observer_ = opt_observer;
  38.  
  39.     /** @type {!Array.<!cr.ui.FocusRow>} */
  40.     this.rows = [];
  41.   }
  42.  
  43.   FocusGrid.prototype = {
  44.     /**
  45.      * Unregisters event handlers and removes all |this.rows|.
  46.      */
  47.     destroy: function() {
  48.       this.rows.forEach(function(row) { row.destroy(); });
  49.       this.rows.length = 0;
  50.     },
  51.  
  52.     /**
  53.      * @param {EventTarget} target A target item to find in this grid.
  54.      * @return {?{row: number, col: number}} A position or null if not found.
  55.      */
  56.     getPositionForTarget: function(target) {
  57.       for (var i = 0; i < this.rows.length; ++i) {
  58.         for (var j = 0; j < this.rows[i].items.length; ++j) {
  59.           if (target == this.rows[i].items[j])
  60.             return {row: i, col: j};
  61.         }
  62.       }
  63.       return null;
  64.     },
  65.  
  66.     /** @override */
  67.     onKeydown: function(keyRow, e) {
  68.       var rowIndex = this.rows.indexOf(keyRow);
  69.       assert(rowIndex >= 0);
  70.  
  71.       var row = -1;
  72.  
  73.       if (e.keyIdentifier == 'Up')
  74.         row = rowIndex - 1;
  75.       else if (e.keyIdentifier == 'Down')
  76.         row = rowIndex + 1;
  77.       else if (e.keyIdentifier == 'PageUp')
  78.         row = 0;
  79.       else if (e.keyIdentifier == 'PageDown')
  80.         row = this.rows.length - 1;
  81.  
  82.       if (!this.rows[row])
  83.         return false;
  84.  
  85.       var colIndex = keyRow.items.indexOf(e.target);
  86.       var col = Math.min(colIndex, this.rows[row].items.length - 1);
  87.  
  88.       this.rows[row].focusIndex(col);
  89.  
  90.       e.preventDefault();
  91.       return true;
  92.     },
  93.  
  94.     /** @override */
  95.     onMousedown: function(row, e) {
  96.       return false;
  97.     },
  98.  
  99.     /**
  100.      * @param {!Array.<!NodeList|!Array.<!Element>>} grid A 2D array of nodes.
  101.      */
  102.     setGrid: function(grid) {
  103.       this.destroy();
  104.  
  105.       this.rows = grid.map(function(row) {
  106.         return new cr.ui.FocusRow(row, this.boundary_, this, this.observer_);
  107.       }, this);
  108.  
  109.       if (!this.getPositionForTarget(document.activeElement) && this.rows[0])
  110.         this.rows[0].activeIndex = 0;
  111.     },
  112.   };
  113.  
  114.   return {
  115.     FocusGrid: FocusGrid,
  116.   };
  117. });
  118.